##########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

# Get the data of the data board

from turtle import begin_fill
from flask import Blueprint
from flask import request
from datetime import timedelta, datetime
import utils.common.log as log
import utils.message.response as response
import json
import utils.config.websvrini as ini
import datetime
import utils.pgsql.base.pgconn as pgconn
import threading
import utils.common.httpclient as httpclient
import utils.pgsql.pgvlogrecord as pgvlogrecord
import utils.pgsql.pgcount as pgCount



user_operat_module = Blueprint(
    "vlog_databoard", __name__, url_prefix="/dataapi/analysis"
)


def deviation_date(date, offsets):
    start_date = datetime.strptime(date, "%Y-%m-%d")
    now_date = timedelta(days=offsets)
    a = start_date + now_date
    return a.strftime("%Y-%m-%d")
# 输入参数
#### input param
# input :
#     {
#     "caseId":"1",  
#     "begin_date":"2022-02-28",
#     "end_date":"2022-03-06"
#     }
#     """

#     ret_str = """
#     {
#     "ref_date":"20220228-20220306",   
#     "caseId":"1",                     
#     "activeUsers":{
#         "totalCount": 100,     # 总数
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}  
            
#         ]
#     },
#     "downLoadUsers":{
#         "totalCount":100,              
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     },
#     "payUsers":{
#         "totalCount":50,              
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     },
#     "payVideos":{                     
#         "totalCount":150,
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}   
#         ]
#     },
#     "composeUsers":{
#         "totalCount":100,              
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     },
#    "composeVideos":{
#         "totalCount":100,               
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     },
#     "shareUsers":{  # 分享
#         "totalCount":100,                
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     },
#     "shareClick":{
#         "totalCount":200,               
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]  
#     },
#     "playClick":{                       
#         "totalCount":200,                
#         "dayInfo":[
#             {"count":10,"time":"2022-02-28"},
#             {"count":20,"time":"2022-03-01"},
#             {"count":15,"time":"2022-03-02"},
#             {"count":25,"time":"2022-03-03"},
#             {"count":25,"time":"2022-03-04"},
#             {"count":25,"time":"2022-03-05"},
#             {"count":25,"time":"2022-03-06"}       
#         ]
#     }        
# }


 
@user_operat_module.route("/getUserPortrait", methods=["POST"])
def getUserPortrait():
    dic = json.loads(request.data)
    end_date = dic["end_date"]
    begin_date = dic["begin_date"]
    endDate = end_date[:4] + end_date[5:7] + end_date[8:]
    beginDate = begin_date[:4] + begin_date[5:7] + begin_date[8:]

    downLoadUsers = {"totalCount": 0, "dayInfo": []}  # 下载
    activeUsers = {"totalCount": 0, "dayInfo": []}  # 活跃用户
    payUsers = {"totalCount": 0, "dayInfo": []}   # 支付
    payVideos = {"totalCount": 0, "dayInfo": []}  # 支付视频
    composeUsers = {"totalCount": 0, "dayInfo": []}
    shareUsers = {"totalCount": 0, "dayInfo": []}  # 分享
    shareClick = {"totalCount": 0, "dayInfo": []}  # 分享点击
    playClick = {"totalCount": 0, "dayInfo": []}

    data = {
        "ref_date": f"{beginDate}-{endDate}",
        "caseId": dic["caseId"],
        "downLoadUsers": downLoadUsers,
        "activeUsers": activeUsers,
        "payUsers": payUsers,
        "payVideos": payVideos,
        "composeUsers": composeUsers,
        "shareUsers": shareUsers,
        "shareClick": shareClick,
        "playClick": playClick,
    }

    while end_date > begin_date:
        tbl = pgCount.PgCount()
        msg = tbl.get(dic["caseId"], end_date)
        if not msg:
            msg = [
                {
                    "payUsers": 0,
                    "shareClickUsers": 0,
                    "downLoadClickTimes": 0,
                    "playTimes": 0,
                    "payTimes": 0,
                    "shareTimes": 0,
                    "productTimes": 0,
                    "productNumber": 0,
                    "userNumber": 0,
                }
            ]
        downLoadUsers["dayInfo"].append(
            {"count": msg[0]["downLoadClickTimes"], "time": end_date}
        )
        downLoadUsers["totalCount"] += msg[0]["downLoadClickTimes"]
        payUsers["dayInfo"].append(
            {"count": msg[0]["payUsers"], "time": end_date})
        payUsers["totalCount"] += msg[0]["payUsers"]
        payVideos["dayInfo"].append(
            {"count": msg[0]["payTimes"], "time+": end_date})
        payVideos["totalCount"] += msg[0]["payTimes"]
        playClick["dayInfo"].append(
            {"count": msg[0]["playTimes"], "time": end_date})
        playClick["totalCount"] += msg[0]["playTimes"]
        shareUsers["dayInfo"].append(
            {"count": msg[0]["shareClickUsers"], "time": end_date}
        )
        shareUsers["totalCount"] += msg[0]["shareClickUsers"]
        shareClick["dayInfo"].append(
            {"count": msg[0]["shareTimes"], "time": end_date})
        shareClick["totalCount"] += msg[0]["shareTimes"]
        composeUsers["dayInfo"].append(
            {"count": msg[0]["productTimes"], "time": end_date}
        )
        composeUsers["totalCount"] += msg[0]["productTimes"]
        activeUsers["dayInfo"].append(
            {"count": msg[0]["userNumber"], "time": end_date})
        activeUsers["totalCount"] += msg[0]["userNumber"]
        end_date = deviation_date(end_date, -1)

    return response.make_response_success(data)

   
    # ret = json.loads(ret_str)
    # return response.make_response_success(ret)


#### ：
# """  input :
# {
# "caseId":"1",  
# "begin_date":"2022-02-28",
# "end_date":"2022-03-06"
# }
# """
@user_operat_module.route("/getTemplateInfo", methods=["POST"])
def getTemplateInfo():
    dic = json.loads(request.data)


    ret_str = """
    {
        "ref_date":"20220228-20220306",   
        "caseId":"1",      
        "TemplateInfo":[
            {
                "templateId":"1",
                "templateName":"muban3+2",
                "composeCount":30,
                "playCount":100,
                "downLoadCount":20

            },
            {
                "templateId":"2",
                "templateName":"muban4+3",
                "composeCount":30,
                "playCount":100,
                "downLoadCount":20

            }
 
        ]  
    }
    """
    ret = json.loads(ret_str)
    return response.make_response_success(ret)
